package copyengine.sceneAndDialog.dialog.impl
{
    import copyengine.sceneAndDialog.CETreeStructureComponent;
    import copyengine.sceneAndDialog.dialog.CEDialogManger;
    import copyengine.sceneAndDialog.dialog.ICEDialog;
    import copyengine.sceneAndDialog.dialog.animation.ICEDialogAnimation;
    import copyengine.utils.CECallbackFunction;
    import copyengine.utils.CEStarlingUtils;

    import flash.geom.Rectangle;

    import starling.core.Starling;
    import starling.display.DisplayObject;
    import starling.display.DisplayObjectContainer;
    import starling.display.Quad;

    /**
     * SimpleDialog 只是对接口的简单封装
     * 1` 默认为模式对话框
     * 2` 提供一个Alpha为0.5黑的背景作为遮罩
     */
    public class CESimpleDialog extends CETreeStructureComponent implements ICEDialog
    {
        protected var mBackground:Quad;
        protected var mDialogAnimation:ICEDialogAnimation;

        private var mIsClosed:Boolean;
        private var duringAnimationDisableOverlay:Quad;
        private var mShowAnimationCallback:CECallbackFunction;
        private var mHideAnimationCallback:CECallbackFunction;

        public function CESimpleDialog()
        {
            super();
        }

        public function onAddToDialogManger():void
        {
            if (mDialogAnimation != null) {
                addDuringAnimationOverlay();
                mShowAnimationCallback = new CECallbackFunction(hideDuringAnimationOverlay);
                mDialogAnimation.playShowAnimation(mShowAnimationCallback);
            }
        }

        public final function close(_isPlayAnimation:Boolean):void
        {
            if (!mIsClosed) {
                mIsClosed = true;
                if (mDialogAnimation != null) {
                    addDuringAnimationOverlay();
                    mHideAnimationCallback = new CECallbackFunction(onCloseAnimationPlayComplete);
                    mDialogAnimation.playCloseAnimation(mHideAnimationCallback);
                } else {
                    onCloseAnimationPlayComplete();
                }
            }
        }

        private function onCloseAnimationPlayComplete():void {CEDialogManger.instance.disposeDialog(this);}


        //=================
        //== DialogAnimation
        //==================
        public function set dialogAnimation(_animation:ICEDialogAnimation):void
        {
            mDialogAnimation = _animation;
            mDialogAnimation.dialog = this;
        }

        public function get dialogAnimation():ICEDialogAnimation {return mDialogAnimation;}


        //=================
        //== DialogContainer
        //=================
        public function get dialogContainer():DisplayObjectContainer {throw new Error("Child class should override this function");}

        public function get isModalDialog():Boolean {return true;}


        //=================
        //== Background
        //=================

        public function get background():DisplayObject
        {
            if (mBackground == null) {
                mBackground = new Quad(Starling.current.stage.stageWidth, Starling.current.stage.stageHeight, backgroundColor);
                mBackground.alpha = backgroundAlpha;
            }
            return mBackground;
        }

        protected function get backgroundColor():uint {return 0x00000;}

        protected function get backgroundAlpha():Number {return 0.5;}


        //====================
        //== DuringAnimation Overlay
        //===================

        private function addDuringAnimationOverlay():void
        {
            duringAnimationDisableOverlay = new Quad(dialogContainer.width, dialogContainer.height, 0xFFFFFF);
            var re:Rectangle = dialogContainer.getBounds(dialogContainer);
            duringAnimationDisableOverlay.x = re.left;
            duringAnimationDisableOverlay.y = re.top;
            duringAnimationDisableOverlay.alpha = 0
            dialogContainer.addChild(duringAnimationDisableOverlay);
        }

        private function hideDuringAnimationOverlay():void
        {
            CEStarlingUtils.removeTargetFromParent(duringAnimationDisableOverlay);
        }

        override public function dispose():void
        {
            if (mHideAnimationCallback != null) {mHideAnimationCallback.isEnable = false;}
            if (mShowAnimationCallback != null) {mShowAnimationCallback.isEnable = false;}
            if (mDialogAnimation != null) {mDialogAnimation.dispose();}
            CEStarlingUtils.removeTargetFromParent(duringAnimationDisableOverlay);
            mHideAnimationCallback = null;
            mShowAnimationCallback = null;
            mDialogAnimation = null;
            super.dispose();
        }
    }
}
